package com.example.chaomi;

import com.droidlogic.app.HdmiCecManager;
import com.droidlogic.app.OutputModeManager;

import android.content.Context;
import android.provider.Settings.Global;

public class CecManager {
	
	
	private Context mContext;
    private static final int DISABLED = 0;
    private static final int ENABLED = 1;
	
	public CecManager(Context context) {
		this.mContext = context;
	}
	
	/**
	 * check HDMI plugged status
	 * @return true means HDMIPlugged
	 */
	public boolean isHDMIPlugged() {
		return (new OutputModeManager(mContext).isHDMIPlugged());
	}
	
	public void switchCec(boolean on) {
        writeCecOption(Global.HDMI_CONTROL_ENABLED, on);
    }

	public void switchOneTouchPlay(boolean on) {
        writeCecOption(HdmiCecManager.HDMI_CONTROL_ONE_TOUCH_PLAY_ENABLED, on);
    }

    public void switchOneTouchPowerOff(boolean on) {
        writeCecOption(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED, on);
    }

    public void switchAutoChangeLanguage(boolean on) {
        writeCecOption(HdmiCecManager.HDMI_CONTROL_AUTO_CHANGE_LANGUAGE_ENABLED, on);
    }
    
    public boolean isSwitchCecOn() {
        return readCecOption(Global.HDMI_CONTROL_ENABLED);
    }

    public boolean isOneTouchPlayOn() {
        return readCecOption(HdmiCecManager.HDMI_CONTROL_ONE_TOUCH_PLAY_ENABLED);
    }

    public boolean isOneTouchPowerOff() {
        return readCecOption(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED);
    }

    public boolean isAutoChangeLanguage() {
        return readCecOption(HdmiCecManager.HDMI_CONTROL_AUTO_CHANGE_LANGUAGE_ENABLED);
    }

    private void writeCecOption(String key, boolean value) {
        Global.putInt(mContext.getContentResolver(), key, toInt(value));
    }

    private boolean readCecOption(String key) {
        return Global.getInt(mContext.getContentResolver(), key, toInt(true)) == ENABLED;
    }
    
    private int toInt(boolean enabled) {
        return enabled ? ENABLED : DISABLED;
    }
}
